Skip to content

fix(ts): Router.predictBatch dropped per-call onPredictStart, onPredictEnd and hooksRaise - #943

Closed
PerryLink wants to merge 1 commit into
NandhaKishorM:mainfrom
PerryLink:fix/ts-batch-hooks
Closed

PerryLink wants to merge 1 commit into
NandhaKishorM:mainfrom
PerryLink:fix/ts-batch-hooks

Conversation

@PerryLink

Copy link
Copy Markdown
Contributor

What this fixes

The batch path composed only part of the per-call hook surface, and type-checked while doing it:

// Router.predict, ~582
const active = composeHooks(this.hooks, opts.hooks, opts.onPredictStart, opts.onPredictEnd);
const raiseErrors = opts.hooksRaise ?? this.hooksRaise;

// Router.predictBatch, ~765  (before)
const active = composeHooks(this.hooks, opts.hooks);
const raiseErrors = this.hooksRaise;

So a per-call onPredictStart or onPredictEnd passed to predictBatch was silently
dropped
— no hook, no error — and a per-call hooksRaise: false had no effect. predictMany
forwarded opts faithfully and so inherited the same gap. Reported in #935.

The Python side of the same asymmetry was closed in #912, which is what makes this a mirror rather
than a redesign.

The fix

PredictBatchOptions gains the three fields, and the batch dispatch site composes them the way
predict does — per-call callbacks after per-call hooks, both after the installed ones — with
hooksRaise overriding the router-level flag for that call only.

Deliberate choices tested:

  • The type grows with the behaviour. The reason this went unnoticed is that the options interface and the composition site agreed with each other and both disagreed with predict. Adding the fields to the interface without wiring them would type-check too, so the tests assert the hooks actually fire rather than that the option is accepted.
  • Both directions of hooksRaise. The router raises by default, so a per-call false is only observable if a throwing hook is swallowed instead of failing the call — and a per-call true on a quiet router must start raising. One direction would pass with the override ignored in the other.
  • Order is pinned, not just membership. A per-call onPredictStart must run after a per-call hooks entry; asserting only that both ran would accept either order.
  • predictMany covered separately, because that forwarding is exactly the property being claimed.

Tests

Five tests in tests/router-batch.test.ts, alongside the existing per-call hooks test.
Confirmed they catch the bug — with only the batch composition reverted (predict untouched),
the file reports 5 failed | 20 passed and every failure is one of the five new tests.

laya-ts:  node node_modules/vitest/vitest.mjs run
          34 files passed, 634 tests passed   (629 before; 5 new)
          tsc -p tsconfig.json --noEmit: 3 error lines with and without this change --
          all Cannot find module 'node:path' / Cannot find name 'process' in providers.ts,
          router.ts and tokenizer.ts, because @types/node is not installed here

Known limitation: the two hooksRaise tests deliberately make a hook throw, so the suite prints
laya: hook Object.onPredictEnd failed: Error: hook blew up to stderr twice. That is the
swallowed failure being asserted, not a test error — the run exits 0.

…PredictEnd` and `hooksRaise`

The batch path composed only part of the per-call hook surface, and type-checked while doing it:

```ts
// Router.predict, ~582
const active = composeHooks(this.hooks, opts.hooks, opts.onPredictStart, opts.onPredictEnd);
const raiseErrors = opts.hooksRaise ?? this.hooksRaise;

// Router.predictBatch, ~765  (before)
const active = composeHooks(this.hooks, opts.hooks);
const raiseErrors = this.hooksRaise;
```

So a per-call `onPredictStart` or `onPredictEnd` passed to `predictBatch` was **silently
dropped** — no hook, no error — and a per-call `hooksRaise: false` had no effect. `predictMany`
forwarded `opts` faithfully to `predictBatch` and so inherited the same gap. Reported in NandhaKishorM#935.

The Python side of the same asymmetry was closed in NandhaKishorM#912, which is what makes this a mirror rather
than a redesign.

## The fix

`PredictBatchOptions` gains the three fields, and the batch dispatch site composes them the way
`predict` does — per-call callbacks after per-call `hooks`, both after the installed ones — with
`hooksRaise` overriding the router-level flag for that call only.

Deliberate choices tested:

- **The type grows with the behaviour.** The reason this went unnoticed is that the options interface and the composition site agreed with each other and both disagreed with `predict`. Adding the fields only to `router.ts`'s interface without wiring them would have type-checked too, so the tests assert the hooks actually fire rather than that the option is accepted.
- **Both directions of `hooksRaise`.** The router raises by default, so a per-call `false` is only observable if a throwing hook is swallowed instead of failing the call — and a per-call `true` on a quiet router must start raising. Asserting one direction would pass with the override ignored in the other.
- **Order is pinned, not just membership.** A per-call `onPredictStart` must run *after* a per-call `hooks` entry, which is the composition `predict` uses; asserting only that both ran would accept either order.
- **`predictMany` is covered separately** even though it forwards, because that forwarding is exactly the property being claimed.

## Tests

Five tests in `tests/router-batch.test.ts`, alongside the existing per-call `hooks` test:

```
honours per-call onPredictStart and onPredictEnd, not just hooks
appends per-call callbacks after per-call hooks, the composition predict uses
lets a per-call hooksRaise override the router-level flag
lets a per-call hooksRaise turn raising on where the router has it off
predictMany honours the same per-call callbacks, since it forwards to predictBatch
```

**Confirmed the tests catch the bug:** with only the batch composition reverted (leaving `predict`
untouched), the file reports `5 failed | 20 passed`, and every failure is one of the five new
tests.

```
laya-ts:  node node_modules/vitest/vitest.mjs run
          34 files passed, 634 tests passed   (629 before; 5 new)
          tsc -p tsconfig.json --noEmit reports 3 error lines with and without this change
          -- all Cannot find module 'node:path' / Cannot find name 'process' in providers.ts,
          router.ts and tokenizer.ts, because @types/node is not installed here
```

Known limitation: the two `hooksRaise` tests deliberately make a hook throw, so the suite prints
`laya: hook Object.onPredictEnd failed: Error: hook blew up` to stderr twice. That line is the
swallowed failure being asserted, not a test error — the run exits 0.
@NandhaKishorM

Copy link
Copy Markdown
Owner

Closing this one, and I want to be clear that it is not because of anything wrong with it. #936 fixes #935 and landed, and your production change and its are the same two lines.

What decided it between them: #936 also forwards hooksRaise into the routeBatch call, so a per-call hooksRaise: false reaches onRoute as well as each request's predict lifecycle. This PR fixes the dispatch site and leaves onRoute on the router-level flag, which leaves half of the documented surface still disagreeing with predict. #936 also updates laya-ts/README.md, where the per-call hook surface is documented. #936 was opened first and by the person who reported #935.

Your tests are better, though, and two of them cover ground #936 does not:

  • A per-call hooksRaise: true turning raising back on where the router has it off. fix(ts): forward per-call hook options through predictBatch #936 tests the false direction and the router-level default, not this one. Your reasoning for why both directions are needed, that one direction passes with the override ignored in the other, is correct.
  • Order pinned rather than membership. Asserting a per-call onPredictStart runs after a per-call hooks entry, rather than only that both ran, is the check that would catch a future reordering of the composeHooks arguments.

I would take both as a test-only follow-up against laya-ts/tests/router-batch.test.ts on current main, with the ctx: never casts dropped since #936 widened PredictBatchOptions and the callbacks are typed now. That is a small PR and I will merge it.

Two other things in here worth keeping on the record. Noting that this went unnoticed because the options interface and the composition site agreed with each other and both disagreed with predict is the correct diagnosis of the class of bug, and it is why your point about adding fields to the interface without wiring them still type-checking matters. And confirming the tests catch the bug by reverting only the batch composition and reporting 5 failed | 20 passed with every failure being one of the five is the strongest form of that evidence.

cgq0816 pushed a commit to cgq0816/laya that referenced this pull request Oct 14, 2026
NandhaKishorM#943 raised two checks that NandhaKishorM#936 does not make, and both were kept on the record
when it was closed in its favour. Neither is in `router-batch.test.ts` today:

- the `true` direction of the per-call `hooksRaise` override, asserting the
  override is honoured when the router has raising off, rather than only that
  the `false` direction is;
- the order of a per-call `onPredictStart` against a per-call `hooks` entry,
  which fails on a swapped `composeHooks` argument where a membership check
  passes either way.

Test-only: no production code changes. The casts NandhaKishorM#943 carried are dropped, since
NandhaKishorM#936 widened `PredictBatchOptions` and the callbacks are typed now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants